﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.factory.ui {
    import flash.events.*;
    import Events.*;
    import flash.utils.*;
    import Display.DialogUI.*;
    import org.aswing.*;
    import Classes.*;
    import Classes.util.*;
    import org.aswing.geom.*;
    import Display.*;
    import flash.text.*;
    import org.aswing.border.*;
    import Classes.inventory.*;
    import org.aswing.ext.*;
    import Display.aswingui.*;
    import L10n.*;
    import com.zynga.skelly.util.*;

    public class FactoryGrowStateDialogView extends GenericDialogView {

        protected static const BUTTON_MARGIN:int = 10;
        protected static const LIST_COLS:int = 2;
        protected static const LIST_ROWS:int = 3;

        protected var m_modelIsDirty:Boolean = true;
        protected var m_disableButtons:Boolean = false;
        protected var m_suppressConfirm:Boolean = false;
        protected var m_model:VectorListModel;
        protected var m_cellFactory:CrewCellFactory;
        protected var m_updateTextCallback:Function;
        protected var m_updateTimerCallback:Function;
        protected var m_updateGoodsCallback:Function;
        protected var m_updateWorkersCallback:Function;
        protected var m_updateButtonsCallback:Function;

        public function FactoryGrowStateDialogView(_arg1:Array, _arg2:CrewCellFactory, _arg3:Dictionary, _arg4:String="", _arg5:String="", _arg6:int=0, _arg7:Function=null, _arg8:String="", _arg9:int=0, _arg10:String="", _arg11:Function=null, _arg12:String=""){
            this.refreshModel(_arg1);
            this.m_cellFactory = _arg2;
            super(_arg3, _arg4, _arg5, _arg6, _arg7, _arg8, _arg9, _arg10, _arg11, _arg12);
        }
        public function update(_arg1:Factory=null):void{
            dispatchEvent(new GenericObjectEvent(UIEvent.REFRESH_DIALOG, _arg1, false, false));
        }
        public function get updateText():Function{
            return (this.m_updateTextCallback);
        }
        public function get updateTimer():Function{
            return (this.m_updateTimerCallback);
        }
        public function get updateGoods():Function{
            return (this.m_updateGoodsCallback);
        }
        public function get updateWorkers():Function{
            return (this.m_updateWorkersCallback);
        }
        public function get updateButtons():Function{
            return (this.m_updateButtonsCallback);
        }
        override protected function makeBackground():void{
            var _local1:MarginBackground;
            if (m_bgAsset){
                _local1 = new MarginBackground(m_bgAsset, new Insets(0, 0, 10, 0));
                this.setBackgroundDecorator(_local1);
                this.setPreferredSize(new IntDimension(m_bgAsset.width, m_bgAsset.height));
                this.setMinimumSize(new IntDimension(m_bgAsset.width, m_bgAsset.height));
                this.setMaximumSize(new IntDimension(m_bgAsset.width, m_bgAsset.height));
            };
        }
        override protected function createInteriorHolder():JPanel{
            var container:* = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP, -10);
            var headerPanel:* = createHeaderPanel();
            var topPanel:* = this.createTopPanel();
            var bottomPanel:* = this.createBottomPanel();
            var buttonPanel:* = this.createButtonPanel();
            var buttonAltPanel:* = this.createButtonAltPanel();
            container.setPreferredSize(new IntDimension(m_bgAsset.width, m_bgAsset.height));
            container.setMinimumSize(new IntDimension(m_bgAsset.width, m_bgAsset.height));
            container.setMaximumSize(new IntDimension(m_bgAsset.width, m_bgAsset.height));
            container.setBorder(new EmptyBorder(null, new Insets(0, 0, 0, 0)));
            container.append(headerPanel);
            container.append(ASwingHelper.verticalStrut(20));
            container.appendAll(topPanel, ASwingHelper.verticalStrut(20), bottomPanel);
            container.append(ASwingHelper.verticalStrut(BUTTON_MARGIN));
            container.append(buttonPanel);
            addEventListener(UIEvent.REFRESH_DIALOG, Curry.curry(function (_arg1:JPanel, _arg2:JPanel, _arg3:JPanel, _arg4:GenericObjectEvent):void{
                var _local5:*;
                if ((_arg4.obj is Factory)){
                    _local5 = Global.factoryWorkerManager.getWorkers((_arg4.obj as Factory).getWorkerBucket()).getRemainingSpots();
                    if (_local5 == 0){
                        _arg1.remove(_arg2);
                        _arg1.append(_arg3);
                        ASwingHelper.prepare(_arg1);
                    };
                };
            }, container, buttonPanel, buttonAltPanel));
            return (container);
        }
        override protected function createButtonPanel():JPanel{
            var container:* = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var btnHireFriendsText:* = m_assetDict.stringAssets.hireFriendsButton;
            var btnBuyWorkerText:* = m_assetDict.stringAssets.buyWorkerButton;
            var buyIcon:* = new AssetIcon(m_assetDict["pic_cashBtn"]);
            var btnHireFriends:* = new CustomButton(btnHireFriendsText.toUpperCase(), null, "GreenButtonUI");
            btnHireFriends.addActionListener(this.onHireFriends, 0, true);
            var btnBuyWorker:* = new CustomButton(btnBuyWorkerText.toUpperCase(), buyIcon, "BigCashButtonUI");
            btnBuyWorker.addActionListener(this.onBuyWorker, 0, true);
            container.appendAll(btnHireFriends, ASwingHelper.horizontalStrut(15), btnBuyWorker);
            addEventListener(UIEvent.REFRESH_DIALOG, Curry.curry(function (_arg1:CustomButton, _arg2:CustomButton, _arg3:FactoryGrowStateDialogView, _arg4:GenericObjectEvent=null):void{
                _arg1.setEnabled(!(_arg3.m_disableButtons));
                if (LocaleHelper.localSetting.sns.cash_enable){
                    _arg2.setEnabled(!(_arg3.m_disableButtons));
                };
            }, btnHireFriends, btnBuyWorker, this));
            return (container);
        }
        protected function createButtonAltPanel():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local2:String = m_assetDict.stringAssets.completeButton;
            var _local3:CustomButton = new CustomButton(_local2.toUpperCase(), null, "GreenButtonUI");
            _local3.addEventListener(MouseEvent.CLICK, this.onOkClick, false, 0, true);
            _local1.append(_local3);
            return (_local1);
        }
        protected function createTopPanel():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
            _local1.setPreferredHeight(155);
            var _local2:JPanel = this.createJobImagePanel();
            var _local3:JPanel = this.createTextArea();
            var _local4:JPanel = this.createJobInfoPanel();
            _local1.appendAll(ASwingHelper.horizontalStrut(20), _local2, ASwingHelper.horizontalStrut(9), _local3, _local4);
            return (_local1);
        }
        protected function createBottomPanel():JPanel{
            var container:* = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var alignContainer:* = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            var gridlist:* = new GridList(this.m_model, this.m_cellFactory, LIST_COLS, LIST_ROWS);
            container.setPreferredHeight(190);
            gridlist.setPreferredWidth(680);
            gridlist.setPreferredHeight(160);
            gridlist.setSelectable(false);
            gridlist.setTileWidth(this.m_cellFactory.preferredCellWidth);
            gridlist.setTileHeight(this.m_cellFactory.preferredCellHeight);
            alignContainer.append(gridlist);
            container.append(alignContainer);
            addEventListener(UIEvent.REFRESH_DIALOG, Curry.curry(function (_arg1:GenericObjectEvent):void{
                if ((_arg1.obj is Factory)){
                    refreshModel((_arg1.obj as Factory).getWorkerData());
                };
            }));
            return (container);
        }
        protected function createJobImagePanel():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.BOTTOM);
            _local1.setPreferredSize(new IntDimension(152, 150));
            var _local2:AssetPane = new AssetPane(m_assetDict["pic_contract"]);
            _local1.append(_local2);
            _local1.append(ASwingHelper.verticalStrut(11));
            return (_local1);
        }
        override protected function createTextArea():JPanel{
            var container:* = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            container.setPreferredSize(new IntDimension(350, 150));
            ASwingHelper.setEasyBorder(container, 15, 5, 20, 5);
            ASwingHelper.prepare(container);
            var txtJobName:* = ASwingHelper.makeTextField(m_assetDict.stringAssets.jobTitle, EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 18, EmbeddedArt.darkBrownTextColor);
            var txtJobDesc:* = ASwingHelper.makeMultilineText(m_assetDict.stringAssets.jobDescription, (container.getPreferredWidth() - 15), EmbeddedArt.DEFAULT_FONT_NAME_BOLD, TextFormatAlign.LEFT, 14, EmbeddedArt.darkBlueTextColor);
            var txtNote:* = ASwingHelper.makeLabel(m_assetDict.stringAssets.unlockWorkerNote, EmbeddedArt.DEFAULT_FONT_NAME, 12, EmbeddedArt.redTextColor, JLabel.LEFT);
            container.appendAll(txtJobName, txtJobDesc);
            container.append(ASwingHelper.verticalStrut(15));
            container.append(txtNote);
            addEventListener(UIEvent.REFRESH_DIALOG, Curry.curry(function (_arg1:AssetPane, _arg2:GenericObjectEvent):void{
                var _local3:*;
                if ((_arg2.obj is Factory)){
                    _local3 = (_arg1.getAsset() as TextField).getTextFormat();
                    (_arg1.getAsset() as TextField).text = getContractDescription((_arg2.obj as Factory));
                    (_arg1.getAsset() as TextField).setTextFormat(_local3);
                };
            }, txtJobDesc));
            return (container);
        }
        protected function createJobInfoPanel():JPanel{
            var container:* = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            var timePanel:* = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var goodsPanel:* = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            container.setPreferredWidth(175);
            timePanel.setPreferredWidth(175);
            goodsPanel.setPreferredWidth(175);
            var picTimer:* = new AssetPane(m_assetDict["pic_timer"]);
            var txtTimer:* = ASwingHelper.makeTextField("", EmbeddedArt.TITLE_FONT, 20, EmbeddedArt.darkBlueTextColor);
            var picGoods:* = new AssetPane(m_assetDict["pic_goods"]);
            var txtGoods:* = ASwingHelper.makeTextField("", EmbeddedArt.TITLE_FONT, 20, EmbeddedArt.greenTextColor);
            txtTimer.setPreferredWidth(150);
            txtGoods.setPreferredWidth(150);
            addEventListener(UIEvent.REFRESH_DIALOG, Curry.curry(function (_arg1:JTextField, _arg2:GenericObjectEvent):void{
                if ((_arg2.obj is Factory)){
                    _arg1.setText(GameUtil.formatMinutesSeconds((_arg2.obj as Factory).getGrowTimeLeft()));
                };
            }, txtTimer));
            addEventListener(UIEvent.REFRESH_DIALOG, Curry.curry(function (_arg1:JTextField, _arg2:GenericObjectEvent):void{
                var _local3:*;
                var _local4:*;
                if ((_arg2.obj is Factory)){
                    _local3 = Global.player.GetDooberMinimums((_arg2.obj as Factory).harvestingDefinition, Commodities.PREMIUM_GOODS_COMMODITY);
                    _local4 = (_local3 * ItemBonus.getBonusModifier((_arg2.obj as Factory), ItemBonus.PREMIUM_GOODS_YIELD));
                    _arg1.setText(_local4.toString());
                };
            }, txtGoods));
            timePanel.appendAll(ASwingHelper.horizontalStrut(15), picTimer, txtTimer);
            goodsPanel.appendAll(ASwingHelper.horizontalStrut(15), picGoods, txtGoods);
            container.appendAll(timePanel, ASwingHelper.verticalStrut(5), goodsPanel);
            return (container);
        }
        protected function refreshModel(_arg1:Array):void{
            var _local2:int;
            var _local3:int;
            var _local4:int;
            var _local5:String;
            if (this.m_modelIsDirty){
                if (this.m_model == null){
                    this.m_model = new VectorListModel();
                };
                this.m_model.clear();
                _local2 = 0;
                while (_local2 < _arg1.length) {
                    _local3 = ((_local2 / LIST_COLS) % 2);
                    _local4 = ((_local2 % LIST_COLS) % 2);
                    _local5 = ((((_local3 + _local4) % 2))==0) ? "cell_bg" : "cell_bg_alt";
                    this.m_model.append({
                        id:_arg1[_local2].id,
                        friendName:_arg1[_local2].friendName,
                        position:_arg1[_local2].position,
                        picUrl:_arg1[_local2].picUrl,
                        bgAsset:_local5
                    });
                    _local2++;
                };
                this.m_modelIsDirty = false;
            };
        }
        protected function onHireFriends(_arg1:Event=null):void{
            this.dispatchEvent(new UIEvent(UIEvent.ASK_FOR_CREW, "", false, true));
        }
        protected function onBuyWorker(_arg1:Event=null):void{
            var e = _arg1;
            var msg:* = ((m_assetDict.stringAssets.unlockWorkerQuestion + " ") + m_assetDict.stringAssets.unlockWorkerNote);
            if (this.m_suppressConfirm){
                this.dispatchEvent(new UIEvent(UIEvent.BUY_CREW, "", false, true));
                this.m_modelIsDirty = true;
            } else {
                this.m_disableButtons = true;
                UI.displayMessage(msg, GenericDialogView.TYPE_YESNO, Curry.curry(function (_arg1:FactoryGrowStateDialogView, _arg2:GenericPopupEvent):void{
                    if (_arg2.button == GenericDialogView.YES){
                        _arg1.dispatchEvent(new UIEvent(UIEvent.BUY_CREW, "", false, true));
                        _arg1.m_modelIsDirty = true;
                        _arg1.m_suppressConfirm = true;
                    };
                    m_disableButtons = false;
                    update();
                }, this), "", true);
            };
            this.update();
        }
        protected function getContractDescription(_arg1:Factory):String{
            var _local2:int = Global.factoryWorkerManager.getWorkers(_arg1.getWorkerBucket()).getRemainingSpots();
            if (_local2 > 0){
                return (ZLoc.t("Items", (_arg1.harvestingDefinition.name + "_description"), {
                    workersLeft:_local2,
                    pos:ZLoc.tk("Items", "worker_position_friendlyName", "", _local2)
                }));
            };
            return (ZLoc.t("Items", (_arg1.harvestingDefinition.name + "_complete")));
        }
        protected function onOkClick(_arg1:MouseEvent=null):void{
            closeMe();
        }

    }
}//package Modules.factory.ui 
